# here put the import lib
# @Time : 2020/8/30 18:39
# @Author : liruiyang
# @File : loginResource
# @Software: PyCharm
from flask import jsonify, g
from flask_login import login_user, logout_user
from flask_restful import reqparse

from ..models.UserMinModel import UserMin
from ..models.AdminInfoModel import AdminInfo
from ..utils import commons
from ..utils.response_code import RET


class UserResource:
    # 登录账号
    @classmethod
    def login(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('Mail', type=str, location='form', required=True, nullable=False,
                            help='Mail参数类型不正确或缺失')
        parser.add_argument('Password', type=str, location='form', required=True, nullable=False,
                            help='Password参数类型不正确或缺失')
        kwargs = parser.parse_args()

        result_dict = AdminInfo.check_password(**kwargs)
        if result_dict.get("code") == "200":
            user_dict = UserMin.get(result_dict['data'].get("UserID"))
            if user_dict.get("code") == '200':
                login_user(user_dict.get("data"))
        return jsonify(code=result_dict['code'], msg=result_dict["message"], data=result_dict.get("data"), error=result_dict.get("error"))

    # 注册账号
    @classmethod
    def register(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserName', type=str, location='form', required=True, nullable=False,
                            help='UserName参数类型不正确或缺失')
        parser.add_argument('Password', type=str, location='form', required=True, nullable=False,
                            help='Password参数类型不正确或缺失')
        parser.add_argument('OtherInfo', type=str, location='form',
                            help='OtherInfo参数类型不正确或缺失')
        parser.add_argument('Mail', type=str, location='form', required=True, nullable=False,
                            help='Mail参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        result_dict = AdminInfo.add_user(**kwargs)
        if result_dict['code'] == 200:
            return jsonify(code=result_dict['code'], message=result_dict["message"], data=result_dict['data'])
        else:
            return jsonify(code=result_dict['code'], message=result_dict["message"], error=result_dict.get("error"))

    # 修改账号
    @classmethod
    def change_password(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('OldPassword', type=str, location='form', required=True, nullable=False,
                            help='OldPassword参数类型不正确或缺失')
        parser.add_argument('NewPassword', type=str, location='form', required=True, nullable=False,
                            help='NewPassword参数类型不正确或缺失')
        kwargs = parser.parse_args()

        args = {
            "UserID": g.user.UserID,
            "Password": kwargs.get("OldPassword")
        }
        check_dict = AdminInfo.check_password(**args)
        if check_dict.get("code") != "200":
            return jsonify(code=check_dict.get("code"), msg=check_dict.get("message"))
        args = {
            "UserID": g.user.UserID,
            "Password": kwargs.get("NewPassword")
        }
        reset_dict = AdminInfo.reset_password(**args)
        if reset_dict.get("code") != "200":
            return jsonify(code=reset_dict.get("code"), msg=reset_dict.get("message"))

        logout_user()
        return jsonify(code=RET.OK, msg="重置密码成功")